package com.wf.wxsign.controller;

import com.wf.wxsign.common.JsonResult;
import com.wf.wxsign.common.utils.HttpUtil;
import com.wf.wxsign.common.utils.JSONUtil;
import com.wf.wxsign.common.utils.UUIDUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created by wangfan on 2018-12-13 下午 4:47.
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/api")
@RestController
public class ApiController {


    // 获取微信JS-SDK签名
    @RequestMapping("/getJSSDKSignature")
    public JsonResult getJSSDKSignature(String appId, String secret, String url) {
        String tokenJson = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + secret, null);
        String access_token = JSONUtil.getString(tokenJson, "access_token");  // access_token
        String ticketJson = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi", null);
        String ticket = JSONUtil.getString(ticketJson, "ticket");  // ticket
        String noncestr = UUIDUtil.randomUUID8();  // 随机字符串
        long timestamp = new Date().getTime();  // 时间戳
        String str = "jsapi_ticket=" + ticket;
        str += "&noncestr=" + noncestr;
        str += "&timestamp=" + timestamp;
        str += "&url=" + url;
        String signature = DigestUtils.sha1Hex(str);
        return JsonResult.ok().put("timestamp", timestamp).put("nonceStr", noncestr).put("signature", signature);
    }

    @RequestMapping("/test")
    public JsonResult getTest(){
        return JsonResult.ok();
    }

    //获取缓存token
    @RequestMapping("/getBaiduToken")
    @Cacheable(cacheNames = "baidu",key = "#root.caches[0].name")
    public JsonResult getBaiduToken(){
//        System.out.println("获取数据");
        return getToken();
    }

    //自动更新token
    @Scheduled(cron = "0 0 0 1/28 * ?" ) //从1号开始 每28天执行一次
    @CachePut(cacheNames = "baidu",key = "#root.caches[0].name")
    public JsonResult updateBaiduToken(){
//        System.out.println("更新数据");
        return getToken();
    }

    private JsonResult getToken() {
        String tokenJson = HttpUtil.get("https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=M8kdMB5e4hp4Ix22CN9v55Cy&client_secret=p1f2F1HeIWYbQtnKRWkHjaasAYgfNYyc", null);
        System.out.println(tokenJson);
        String accessToken = JSONUtil.getString(tokenJson, "access_token");
        return JsonResult.ok().put("accessToken", accessToken);
    }


    /**
     * access_token请求接口：
     * https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxx&secret=xxx
     * 返回示例：
     * {"access_token":"xxxxxx","expires_in":7200}
     *
     * ticket请求接口：
     * https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=xxx&type=jsapi
     * 返回示例：
     * {"errcode":0,"errmsg":"ok","ticket":"xxxxx","expires_in":7200}
     */
}
